package main

import (
	"fmt"

	"gitee.com/meilier/flower/equivalence"
	"gitee.com/meilier/flower/image"
	"gitee.com/meilier/flower/platform"
	"gitee.com/meilier/flower/util"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

type flowOptions struct {
	imageRequireSpec     string
	platformProviderSpec string
}

var flowOpts flowOptions = flowOptions{}

// NewRunCmd returns run command
func newFlowCmd() *cobra.Command {
	runCmd := &cobra.Command{
		Use:     "flow [FLAGS]",
		Short:   "get compatibility result",
		Example: `flower flow --image "/root/image.spec" --platform "/root/platform.spec"`,
		RunE:    flowCommand,
	}

	runCmd.PersistentFlags().StringVarP(&flowOpts.imageRequireSpec, "image", "i", "", "image require spec path")
	runCmd.PersistentFlags().StringVarP(&flowOpts.platformProviderSpec, "platform", "p", "", "platform provider spec path")

	return runCmd
}

func flowCommand(c *cobra.Command, args []string) error {
	if len(args) != 0 {
		return errors.New("no arguments needed here, please use flags")
	}

	satisfiability := equivalence.Specv1("")

	imageSpec := image.RequireSpec{}
	if err := util.LoadJSONFile(flowOpts.imageRequireSpec, imageSpec); err != nil {
		return err
	}

	platformSpec := platform.ProviderSpec{}
	if err := util.LoadJSONFile(flowOpts.platformProviderSpec, imageSpec); err != nil {
		return err
	}

	if satisfiability.Equals(imageSpec, platformSpec) {
		fmt.Println("True")
	}
	fmt.Println("False")

	return nil
}
